'use strict';
require('./index.css')
require('page/common/nav-simple/index.js')
var _mm = require('util/mm.js')
var _user = require('service/user-service.js')

//表单的错误信息
var fromError = {
    show : function(errMsg){
        $('.err-item').show().find('.error-msg').text(errMsg)
    },
    hide : function(){
        $('.err-item').hide().find('.error-msg').text('')
    }
}
//登录页的逻辑部分
var page = {
    init : function(){
        this.bindEvent()
    },
    bindEvent : function(){
        var _this = this;
        $('#submit').click(function(){
            _this.submit()
        })
        $('.user-content').keyup(function(e){
            if(e.keyCode===13){
                _this.submit()
            }
        })
    },
    //提交表单
    submit : function(){
        var formData = {
            username : $.trim($('#username').val()),
            password : $.trim($('#password').val())
        }
        //validateResult 表单验证结果
         var validateResult = this.formValidate(formData);
        //验证成功
        if(validateResult.status){
            //提交
            _user.login(formData,function(){
                window.location.href = _mm.getUrlParam('redirect') || './index.html'
            },function(errMsg){
                fromError.show(errMsg)
            })
        }
        //验证失败
        else{
                fromError.show(validateResult.msg)
        }
    },
    //表单字符的验证
    formValidate : function(formData){
        var result = {
            status : false,
            msg    : ''
        }   
        if(!_mm.validate(formData.username,'require')){
            result.msg = '用户名不能为空'
            return result
        }
         if(!_mm.validate(formData.password,'require')){
            result.msg = '密码不能为空'
            return result
        }
        //通过验证 返回正确提示
        result.status = true ;
        result.msg = '验证通过';
        return result;

    }

}

$(function(){
    page.init()
})